﻿using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;

namespace SimpleORM.Infrastructure.Swagger
{

    /// <summary>
    /// 请求签名验证添加Swagger请求头
    /// </summary>
    public class RequestValidSignSwaggerOperationFilter : IOperationFilter
    {
        public void Apply(OpenApiOperation operation, OperationFilterContext context)
        {
            if (operation.Parameters == null)
                operation.Parameters = new List<OpenApiParameter>();
            //应用ID，非必填，扩展使用
            operation.Parameters.Add(new OpenApiParameter
            {
                Name = "AppId",
                In = ParameterLocation.Header,
                Required = false,
                Description = "应用ID",
                Schema = new OpenApiSchema
                {
                    Type = "string"
                }
            });
            operation.Parameters.Add(new OpenApiParameter
            {
                Name = "Ticks",
                In = ParameterLocation.Header,
                Required = true,
                Description = "时间戳",
                Example = new OpenApiString(((long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds).ToString()),
                Schema = new OpenApiSchema
                {
                    Type = "string"
                }
            });
            operation.Parameters.Add(new OpenApiParameter
            {
                Name = "RequestId",
                In = ParameterLocation.Header,
                Required = true,
                Description = "请求ID",
                Example = new OpenApiString(Guid.NewGuid().ToString()),
                Schema = new OpenApiSchema
                {
                    Type = "string"
                }
            });
            operation.Parameters.Add(new OpenApiParameter
            {
                Name = "Sign",
                In = ParameterLocation.Header,
                Required = true,
                Description = "请求签名",
                //{AppId}{Ticks}{RequestId}{AppSecret}
                Example = new OpenApiString(""),//MD5({AppId}{Ticks}{RequestId}{AppSecret})
                Schema = new OpenApiSchema
                {
                    Type = "string"
                }
            });
            operation.Parameters.Add(new OpenApiParameter
            {
                Name = "Method",
                In = ParameterLocation.Header,
                Required = true,
                Description = "请求资源",
                Example = new OpenApiString("/TWarehouse/GETTWERPConfirmInformation"),//MD5({AppId}{Ticks}{RequestId}{AppSecret})
                Schema = new OpenApiSchema
                {
                    Type = "string"
                }
            });
            operation.Parameters.Add(new OpenApiParameter
            {
                Name = "Req_id",
                In = ParameterLocation.Header,
                Required = false,
                Description = "租户ID",
                Example = new OpenApiString(""),//MD5({AppId}{Ticks}{RequestId}{AppSecret})
                Schema = new OpenApiSchema
                {
                    Type = "string"
                }
            });
            operation.Parameters.Add(new OpenApiParameter
            {
                Name = "RDmber",
                In = ParameterLocation.Header,
                Required = true,
                Description = "随机数",
                Example = new OpenApiString(RandomNumber()),
                Schema = new OpenApiSchema
                {
                    Type = "string"
                }
            });
            operation.Parameters.Add(new OpenApiParameter
            {
                Name = "AppSecret",
                In = ParameterLocation.Header,
                Description = "应用密钥(调试用)",
                Example = new OpenApiString(""),//BASE64({AppSecret})  ToBase64String("123456")  MTIzNDU2
                Schema = new OpenApiSchema
                {
                    Type = "string"
                }
            });
        }
        //public static string ToBase64String(object obj)
        //{
        //    if (obj == null)
        //        throw new ArgumentNullException(nameof(obj));

        //    // 使用二进制序列化将对象转换为字节数组
        //    //var formatter = new BinaryFormatter();
        //    using (var memoryStream = new MemoryStream())
        //    {
        //        formatter.Serialize(memoryStream, obj);
        //        var bytes = memoryStream.ToArray();

        //        // 使用 Convert.ToBase64String 将字节数组转换为 Base64 字符串
        //        return Convert.ToBase64String(bytes);
        //    }
        //}

        public static string RandomNumber()
        {
            Random random = new Random();
            // 生成随机数
            return random.Next(100000000, 999999999).ToString();
        }
    }
}
